//
//  BVIdexViewController.m
//  AssistantCloud1
//
//  Created by YYKJ on 2017/4/15.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVTextView.h"

@implementation BVTextView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUI];
        
    }
    return self;
}
- (void)setUI{
    
    self.numLabel = [[UILabel alloc] init];
    self.numLabel.textAlignment = NSTextAlignmentCenter;
    self.numLabel.font = [UIFont systemFontOfSize:14];
    self.numLabel.textColor = [UIColor blackColor];
    [self addSubview:self.numLabel];
    self.numLabel.sd_layout
    .bottomSpaceToView(self,5)
    .rightSpaceToView(self,5)
    .widthIs(50)
    .heightIs(30);
    
    // 当UITextView的文字发生改变时，UITextView自己会发出一个UITextViewTextDidChangeNotification通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];

    
    CGFloat SCREEN_WIDTH = [UIScreen mainScreen].bounds.size.width;
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,44)];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 60, 7,50, 30)];
    [button setTitle:@"完成"forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
    
    [bar addSubview:button];
    self.inputAccessoryView = bar;
    
    [button addTarget:self action:@selector(print)forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark -- 当文本发生变化时调用
- (void)textDidChange {
    // 重新调用(重绘)
    [self setNeedsDisplay];
    
}

#pragma mark -- 重写setter方法
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = [placeholder copy];
    
    [self setNeedsDisplay];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    
    [self setNeedsDisplay];
}

- (void)setFont:(UIFont *)font {
    [super setFont:font];
    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    
    // setNeedsDisplay会在下一个消息循环时刻，调用drawRect:
    [self setNeedsDisplay];
}

/***   利用绘画画一个矩形框显示文字*/
- (void)drawRect:(CGRect)rect {
    // 如果有输入文字，就直接返回，不重新绘画
    if (self.hasText) return;
    
    // 文字属性
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    // 字体默认为textView的字体大小
    attrs[NSFontAttributeName] = self.font?self.font:[UIFont systemFontOfSize:14];
    // 字体颜色
    attrs[NSForegroundColorAttributeName] = self.placeholderColor?self.placeholderColor:[UIColor grayColor];
    // 画文字
    CGFloat textX = 5;
    CGFloat textY = 8;
    CGFloat textW = rect.size.width - 2 * textX;
    CGFloat textH = rect.size.height - 2 * textY;
    [self.placeholder drawInRect:CGRectMake(textX, textY, textW, textH) withAttributes:attrs];
    
}

- (void)print {
    
    [self resignFirstResponder];
}

#pragma mark -- 移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
